package com.budwk.app.sqfwV2.generator.binyangStreet.pdf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BasePdfParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.component.ProblemComponent;
import com.budwk.app.sqfwV2.component.SignImageComponent;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskPdfDTO;
import com.budwk.app.sqfwV2.enums.BinYangFileServiceEnum;
import com.budwk.app.sqfwV2.enums.centuryCitySafe.CampusTrainingProblemsEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addWordUserSignature;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-05-06
 *
 * 宾阳街道安全巡查2期 safe_check_task
 * 校外培训机构安全排查.docx
 */
public class CampusTrainingOffDoc extends BaseTemplatePdfGenerator {

    @Override
    public String getTemplateName() {
        return BinYangFileServiceEnum.THREE_PDF.getFilePath();
    }

    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfGenerateParam) {
        SafeCheckTaskPdfDTO pdfDTO = (SafeCheckTaskPdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        return BinYangFileServiceEnum.THREE_PDF.getLabel().equals(pdfDTO.getType());
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfParamBO) {
        SafeCheckTaskPdfDTO pdfDTO = ProblemComponent.getSafetyPdfDTO(pdfParamBO);
        // 构建基本参数
        buildBaseMap(paramMap, pdfDTO, pdfParamBO);
        // 构建问题
        buildProblemMap(paramMap, pdfDTO);
        buildOther(paramMap, pdfDTO, pdfParamBO.getBasePdfParamDTO());
    }

    private void buildBaseMap(Map<String, Object> paramMap, SafeCheckTaskPdfDTO pdfDTO, SqfwPdfCreateCommonBO pdfParamBO) {
        paramMap.put("inspectionDate", DateUtil.format(pdfParamBO.getBasePdfParamDTO().getFillDate(), DatePattern.NORM_DATE_PATTERN));
        paramMap.put("placeName", StrUtil.formatStr(pdfDTO.getPlaceName()));
        paramMap.put("address", StrUtil.formatStr(pdfDTO.getAddress()));
        paramMap.put("siteManager", StrUtil.formatStr(pdfDTO.getSiteManager()));
        paramMap.put("operatorTelephone", StrUtil.formatStr(pdfDTO.getOperatorTelephone()));
        paramMap.put("createName", StrUtil.formatStr(pdfParamBO.getBasePdfParamDTO().getCreateUserName()));
        paramMap.put("community", StrUtil.formatStr(pdfParamBO.getBasePdfParamDTO().getCommunity()));
    }

    private void buildProblemMap(Map<String, Object> paramMap, SafeCheckTaskPdfDTO pdfDTO) {
        List<SafeCheckTaskParamDataDTO> hiddenDanger = pdfDTO.getHiddenDanger();
        List<SafeCheckTaskParamDataDTO> defaultValues = CampusTrainingProblemsEnum.getDefaultValues();
        for (SafeCheckTaskParamDataDTO defaultValue : defaultValues) {
            Optional<SafeCheckTaskParamDataDTO> otional = hiddenDanger.stream().filter(e -> defaultValue.getFields().equals(e.getFields())).findFirst();
            if (otional.isEmpty()) {
                paramMap.put(defaultValue.getFields(), getFieldsValue(defaultValue.getKey()));
                continue;
            }
            paramMap.put(defaultValue.getFields(), getFieldsValue(otional.get().getKey()));
        }
    }

    private Boolean getFieldsValue(String key) {
        return key.equals(CommonConstants.YES_CHINA);
    }

    private void buildOther(Map<String, Object> paramMap, SafeCheckTaskPdfDTO pdfDTO, BasePdfParamDTO basePdfParamDTO) {
        // 检查人员签字
        SignImageComponent.signWord(paramMap, 1.0F, pdfDTO.getCheckedUnitLeader(), basePdfParamDTO.getCreateUserName(), "checkedUnitLeader", "checkedUnitLeaderStr");
        // 2 获取现场管理人员签名
        buildUserSign(basePdfParamDTO, paramMap);
    }

    private void buildUserSign(BasePdfParamDTO basePdfParamDTO, Map<String, Object> paramMap) {
        SysUserService userService = NutzIocUtils.getBean(SysUserService.class);
        Sys_user user = userService.fetch(basePdfParamDTO.getCreateUserId());
        String createUserSigUrl = user.getSignatureUrl();
        List<String> createUsernameList = Lists.newArrayList();
        List<String> createUserSigUrlList = Lists.newArrayList();
        if (StringUtils.isBlank(createUserSigUrl)) {
            createUsernameList.add(user.getUsername());
        } else {
            createUserSigUrlList.add(createUserSigUrl);
        }
        addWordUserSignature(paramMap, "cuNoSig", "cuSigs", "cuSig", createUsernameList, createUserSigUrlList, 1.0F);
    }
}
